package net.slenderloris.TouchServer;

import java.awt.Frame;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;

import net.slenderloris.TouchServer.GestureEventGenerator.OnTapListener;

public class TouchServerMain {

	private static final int ACTION_UP = 0x1;
	private static final int ACTION_DOWN = 0x2;
	private static final int ACTION_MOVE = 0x3;
	
	private FingerCanvas mFingerCanvas;
	private GestureEventGenerator mGestureEventGenerator;
	private KeyboardTrainer mKeyboardTrainer;
	private int mTapCount;
	private String mTypedString;

	private class ListenRunnable implements Runnable {
		@Override
		public void run() {
			byte[] bytes = new byte[10];
			BtSocketServer server = null;
			DataInputStream dis = null;

			try {
				server = new BtSocketServer();
				dis = server.getBtInputStream();
				while(true) {

					int read = dis.read(bytes, 0, 10);
					
					//System.out.println("read " + read + " " + bytesToString(bytes));

					ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
					DataInputStream dis2 = new DataInputStream(bis);
					
					byte action = dis2.readByte();
					byte id = dis2.readByte();
					int time = dis2.readInt();
					short x = dis2.readShort();
					short y = dis2.readShort();

					String actionStr = actionToString(action);
					
					//System.out.println("A " + actionStr + " (" + id + ") " + x + "x" + y + " [" + time + "]");
					
					switch(action) {
					case ACTION_DOWN:
						mGestureEventGenerator.fingerDown(id, time, x, y);
					case ACTION_MOVE:
						mFingerCanvas.setPosition(id, x, y);
						break;
					case ACTION_UP:
						mGestureEventGenerator.fingerUp(id, time, x, y);
						mFingerCanvas.fingerUp(id);
						break;
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if(server!=null) { try { server.close(); } catch (IOException e1) {} }
				if(dis!=null) { try { dis.close(); } catch (IOException e1) {} }
			}
		}
	}
	
	private String bytesToString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for(byte b: bytes) {
			sb.append(b);
			sb.append(" ");
		}
		return sb.toString();
	};

	
	public static void main(String[] args) {
		System.out.println("Hello World!"); // Display the string.
		TouchServerMain server = new TouchServerMain();
		server.start();
	}
		
	public void start() {
		
		new Thread(new ListenRunnable()).start();
		
		mKeyboardTrainer = new KeyboardTrainer();
		mTapCount = 0;

		System.out.println("TAP: " + mKeyboardTrainer.getCurrentLetter());
		
		mTypedString = "";

		mFingerCanvas = new FingerCanvas();
		mFingerCanvas.setKeyboardTrainer(mKeyboardTrainer);		
		
		mGestureEventGenerator = new GestureEventGenerator();
		mGestureEventGenerator.setOnTapListener(new OnTapListener() {
			@Override
			public void onTap(int x, int y) {
				mFingerCanvas.setTapPoint(x,y);
				mTapCount++;

				Point tapPoint = new Point(x,y);
				mKeyboardTrainer.tapped(tapPoint);

				mTypedString = mTypedString + mKeyboardTrainer.getNearestLetter(tapPoint);
				mFingerCanvas.setTypedText(mTypedString);
				if(mTypedString.length()>36) {
					mTypedString = "";
				}
				
				Toolkit.getDefaultToolkit().beep();
			}
		});
		
		Frame aFrame = new Frame(); 
		aFrame.setSize(1280, 800); 
		//add the canvas 
		aFrame.add(mFingerCanvas);
		aFrame.setVisible(true);
		aFrame.invalidate();
		
	}

	public static String actionToString(byte action) {
		switch(action) {
		case ACTION_UP: return "Up";
		case ACTION_DOWN: return "Down";
		case ACTION_MOVE: return "Move";
		}
		return null;
	}

}
